const Pool = require('./../mysql/mysql');
const connect = Pool.Pool;

/*
* 用户表的增删改查
* */
function userService(){
    this.login = (data,callback) => {
    //登录
        let sql = 'select * from users where account = '+ data;
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    this.register = (data,callback) => {
    //注册
        let arr = [];//将职工对象的所有属性转化为数组
        arr.push(data.account);
        arr.push(data.psd);
        //插入用户的sql语句
        let sql = "insert into users(account,psd) values (?,?)";
        connect.getConnection(function (err, connection) {
            if(err){
                console.log('数据库连接失败');
                return;
            }else{
                connection.query(sql,arr,function (err, rows, fields) {
                    if(err) {
                        console.log('插入用户失败：' + err.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(rows);
                });
            }
        })
    }
    this.queryAccount = (account, callback) => {
        let sql = 'select * from users where account = ' + account;
        //和数据库连接池建立连接
        connect.getConnection(function (err, connection) {
            if(err){
                console.log('连接失败');
                return;
            }else{
                connection.query(sql,function (error, row, field) {
                    if(error){
                        console.log('查询失败' + error.message);
                        return;
                    }
                    connection.release(); //每一次用完都要释放
                    callback(row);
                });
            }
        });
    }
    this.upload = (params,callback) => {
        //上传头像
        let sql = 'UPDATE users SET portrait= ? where account= ?';
        let data = [params.images,params.account];
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,data,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    this.queryAllUsers = (callback) => {
        let sql = 'select * from users';
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    this.frezee = (params,callback) => {
        let type = params.type,sql = null;
        if (type === 'frezee'){
            let time = new Date().getTime() + 60*48*60*1000
            sql = 'update users set frazenDate='+time+' where account='+params.account;
        } else{
            let time = null;
            sql = 'update users set frazenDate='+time+' where account='+params.account;
        }
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    this.batch = (arr,callback) => {
        let time = new Date().getTime() + 60*48*1000*60;
        let data = arr.split(',');
        data.forEach(item => {
            let sql = 'update users set frazenDate='+time+' where id=?';
            connect.getConnection((err,connection) => {
                if (err){
                    console.log('连接失败');
                    return;
                } else{
                    connection.query(sql,item,(error,rows,field) => {
                        if (error){
                            console.log(error);
                            return;
                        }else{
                            connection.release(); //每一次用完都要释放
                            callback(rows);
                        }
                    })
                }
            })
        })
    }
    this.search = (params,callback) => {
        let sql = 'select * from users where '+ params.key + ' like '+ params.value;
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
    this.setInfo = (params, callback) => {
        let sql = "UPDATE users set sex=?,major=?,nation=?,team=?,class=?,captain=?,nickName=?,sign=?,address=?,name=?,idNum=?,college=?,room=? where account=?";
        let data_ = params.body;
        let data = [data_.sex,data_.major,data_.nation,data_.team,data_.class,data_.captain,data_.nickName,data_.sign,data_.address,data_.name,data_.idNum,data_.college,data_.room,data_.account];
        connect.getConnection((err,connection) => {
            if (err){
                console.log('连接失败');
                return;
            } else{
                connection.query(sql,data,(error,rows,field) => {
                    if (error){
                        console.log(error);
                        return;
                    }else{
                        connection.release(); //每一次用完都要释放
                        callback(rows);
                    }
                })
            }
        })
    }
};

module.exports=userService;
